package dao.impl;

import dao.DeptDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.Dept;
//import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoimpl implements DeptDao {
    List<Dept> users=new ArrayList<>();
    Connection connection=null;
    PreparedStatement pst=null;
    ResultSet resultSet=null;

    @Override
    public List<Dept> findAll() {
        //定义mybatis核心配置文件
        String resource = "mybatis-config.xml";
        SqlSession sqlSession = null;
        //加载配置文件
        try {
            //流操作
           InputStream inputStream = Resources.getResourceAsStream(resource);
           //会话工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //创建会话
            sqlSession = sqlSessionFactory.openSession();
            //执行数据库操作
            List<Dept> depts = sqlSession.getMapper(DeptDao.class).findAll();
            return depts;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
    }
}
